#include "heap_FK.h"
#include <vector>
#include <iostream>
using namespace std;

static void
do_tests(heap876 &heap)
{
  vector<void *> allocated;
  vector<size_t> allocated_bytes;
  vector<unsigned> tags;
  int  usedTags[10] = {0,0,0,0,0,0,0,0,0,0};
  for (int i = 0; i < 100; i++) {
    size_t t = rand() % 10240;
	unsigned tag = rand() % 10;
	usedTags[tag] = 1;
    void *a = heap.alloc(t, tag);
    allocated.push_back(a);
    allocated_bytes.push_back(t);
	tags.push_back(tag);
  }

//  for (size_t i = 0; i < allocated.size(); i++) {
//    size_t t = heap.allocated_size(allocated[i]);
//    if (t != allocated_bytes[i]) {
//      cout << t << endl;
//    }
//  }
  cout << "Occupancy before all = " << heap.get_occupancy() << endl;

  for (unsigned tt = 0; tt < 10; tt++)
  {
	  if (usedTags[tt]) {
		  heap.free_all(tt);
		  cout << "Occupancy after free_all(" << tt << ")= " << heap.get_occupancy() << endl;
	  }
  }
  for (size_t i = 0; i < allocated.size(); i++) {
    heap.free(allocated[i]); // , tags[i]);
    allocated[i] = NULL;
    cout << "Occupancy after free(" << i << ")= " << heap.get_occupancy() << endl;
  }
}

static void do_debug_tests(heap876 &h)
{
	char * bad = (char *)h.alloc(1000);
	*bad = '!';
	h.free(bad);
	cout << *bad << endl;
}

int main()
{
	heap876 *h = new heap_FK(1024000, heap876::alloc_simple, "FK");
    do_tests(*h);
	delete h;
	heap876 * d = new heap_FK(100000, heap876::alloc_debug, "FKD");
	do_debug_tests(*d);
	delete d;
}
